package com.china08.yxyapi.rabbitmq.listener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.china08.yxyapi.config.RabbitConfig;
import com.china08.yxyapi.service.jpush.impl.JpushServiceImpl;

@Component
@EnableRabbit
public class JpushForMessageListener {

    @Autowired
    JpushServiceImpl jpushService;

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_MESSAGE)
    public void listener1(Map<String, String> bodyMap) {
        sendMessageReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_MESSAGE)
    public void listener2(Map<String, String> bodyMap) {
        sendMessageReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_MESSAGE)
    public void listener3(Map<String, String> bodyMap) {
        sendMessageReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_MESSAGE)
    public void listener4(Map<String, String> bodyMap) {
        sendMessageReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_MESSAGE)
    public void listener5(Map<String, String> bodyMap) {
        sendMessageReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_MESSAGE)
    public void listener6(Map<String, String> bodyMap) {
        sendMessageReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_MESSAGE)
    public void listener7(Map<String, String> bodyMap) {
        sendMessageReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_MESSAGE)
    public void listener8(Map<String, String> bodyMap) {
        sendMessageReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_MESSAGE)
    public void listener9(Map<String, String> bodyMap) {
        sendMessageReady(bodyMap);
    }

    @RabbitListener(queues = RabbitConfig.JPUSH_FOR_MESSAGE)
    public void listener10(Map<String, String> bodyMap) {
        sendMessageReady(bodyMap);
    }

    /**
     * 发送数据前准备
     * 
     * @param bodyMap
     */
    private void sendMessageReady(Map<String, String> bodyMap) {
        String msgReceivers = bodyMap.get("msgReceivers");
        String msgContent = bodyMap.get("msgContent");
        String msgTag_and = bodyMap.get("msgTag_and");
        String date = bodyMap.get("date");

        // 电子书包来源
        String flag = bodyMap.get("flag");

        // 接收者集合
        String[] msgReceiversArr = msgReceivers.split(",");
        List<String> msgReceiversLst = Arrays.asList(msgReceiversArr);

        String[] msgTag_andArr = null;
        if (msgTag_and != null) {
            msgTag_andArr = msgTag_and.split(",");
        }

        String[] tags = new String[0];

        // 分组发送
        List<String> msgReceiversLstTmp = new ArrayList<>();
        if (StringUtils.isEmpty(date)) {
            for (String msgReceiver : msgReceiversLst) {
                // 临时集合
                msgReceiversLstTmp.add(msgReceiver);
                // 20
                if (msgReceiversLstTmp.size() == 20) {
                    // 发送
                    tags = msgReceiversLstTmp.toArray(new String[0]);
                    jpushService.sendMessagePushToTags(msgContent, tags, msgTag_andArr);
                    // 重置
                    msgReceiversLstTmp = new ArrayList<>();
                    tags = new String[0];
                }
            }
            // 不够20条发送一次
            tags = msgReceiversLstTmp.toArray(new String[0]);
            jpushService.sendMessagePushToTags(msgContent, tags, msgTag_andArr);
        } else {
            // 定时推送
            for (String msgReceiver : msgReceiversLst) {
                // 临时集合
                msgReceiversLstTmp.add(msgReceiver);
                // 20
                if (msgReceiversLstTmp.size() == 20) {
                    // 发送
                    tags = msgReceiversLstTmp.toArray(new String[0]);
                    jpushService.sendMessagePushToTagsTiming(msgContent, tags, msgTag_andArr, date, flag);
                    // 重置
                    msgReceiversLstTmp = new ArrayList<>();
                    tags = new String[0];
                }
            }
            // 不够20条发送一次
            tags = msgReceiversLstTmp.toArray(new String[0]);
            jpushService.sendMessagePushToTagsTiming(msgContent, tags, msgTag_andArr, date, flag);
        }

    }
}
